
const express = require("express");
const static = require('serve-static');
const bodyParer  = require('body-parser');
const config = require('./config');
const util = require('./util');
const app = express();
const port = process.argv[2] || config.port;

app.all('*', function(req, res, next) {
  //代理服务器允许跨域
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With,content-type,token");
  //res.header("Access-Control-Allow-Headers", "*");
  res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
  res.header("Content-Type", "application/json;charset=utf-8");
  res.header("X-Powered-By",' pdf-server');
  next();
});

app.use(bodyParer.json({limit: config.limit}));
app.use(bodyParer.urlencoded({limit: config.limit, extended: true}));
app.use('/', require('./router'));
config.staticDirs.forEach(function(staticDir){
  app.use(staticDir, static(__dirname + staticDir));
});


const server = app.listen(port, function () {
  console.log(new Date().format('yyyy-MM-dd hh:mm.ss') + ' \033[42;30m HTTP Server Started Listen Port ' + port + ' \033[0m');
});